package com.whz.enummap.plant;

import java.util.Arrays;
import java.util.EnumMap;
import java.util.Set;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toSet;

/**
 * @Author: sinosoft
 * @Description: 使用stream表达式并指定EnumMap 以提高性能
 * @Date: Create in 10:25 2021/5/17
 * @Modified By:
 */
public class PlantMainTest_EnumMapStream {
    public static void main(String[] args) {
        Plant[] garden = new Plant[] {
                new Plant("白菊花", Plant.LifeCycle.ANNUAL),
                new Plant("黄菊花", Plant.LifeCycle.ANNUAL),
                new Plant("橄榄菊", Plant.LifeCycle.BIENNIAL),
//                new Plant("银冠花", Plant.LifeCycle.PERENNIAL)
        };

        EnumMap<Plant.LifeCycle, Set<Plant>> lifeCycleSetEnumMap =
                Arrays.stream(garden).collect(groupingBy(p -> p.lifeCycle,
                                                () -> new EnumMap<>(Plant.LifeCycle.class),
                                                toSet()));
        System.out.println(lifeCycleSetEnumMap);
    }
}
